<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############



if(get('conn')==''){
    // need to select a connection

    $conns = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_servers ORDER BY url ASC");

    if(!is_array($conns) || count($conns)==0){
        echo '<p>No connections yet. <a href="'.$this->adminUrl.'/client-settings/new-connection/">Create one now</a></p>';
    }else{
        echo '<table><tbody>';
        foreach($conns as $k=>$v){
            echo '<tr><td>'.$this->getContentLang('conn_title', $v['id']).'</td>
                <td><a href="'.$this->adminUrl.'/client-settings/edit-connection/?conn='.$v['id'].'">edit</a></td>
                <td><a href="'.$this->adminUrl.'/client-settings/edit-connection/?conn='.$v['id'].'&amp;delete=1">delete</a></td></tr>';
        }
        echo '</tbody></table>';

    }
}else{
    $connid = (int) get('conn');

    if((int) get('delete')>0){
        $DB->delete($CONFIG->dbprefix.'vms_servers', "id = '{$connid}'");
        $DB->delete($CONFIG->dbprefix.'vms_subscriptions', "connection_id = '{$connid}'");
        JFX::addSuccess('Connection deleted');
        JFX::redirect($this->adminUrl);
    }



    $cData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}vms_servers WHERE id = '{$connid}'");

    if(!is_array($cData) || count($cData)==0){
        JFX::addError('Invalid connection');
        JFX::redirect($this->adminUrl.'/client-settings/edit-connection/');
    }

    // now we have a valid connection


    if(Antz_IntelliForm::submitted('edit-connection')){
        $name = post('name');
        $url = post('url');
        $username = post('username');
        $password = post('password');
        $salt = post('cryptSalt');

        $error = false;

        if(!$error){
            $dbParams = array(
                'url' => $url,
                'username' => $username,
                'pass' => $CRYPT->crypt($password),
                'crypt_salt' => $CRYPT->crypt($salt)
            );
            $DB->update($CONFIG->dbprefix.'vms_servers', $dbParams, "id = '{$connid}'");
            $this->updateContentLang('conn_title', $connid, $name);
            JFX::addSuccess('Update success!');
            JFX::redirect(THIS_PAGE_URL);
        }
    }



    $formFields = array(
        array(
            'type' => 'text',
            'name' => 'name',
            'value' => post('name', $this->getContentLang('conn_title', $connid)),
            'label' => 'Connection Name'
        ),
        array(
            'type' => 'text',
            'name' => 'url',
            'value' => post('url', $cData['url']),
            'label' => 'URL'
        ),
        array(
            'type' => 'text',
            'name' => 'username',
            'value' => post('username', $cData['username']),
            'label' => 'Username'
        ),
        array(
            'type' => 'password',
            'name' => 'password',
            'value' => post('password', $CRYPT->decrypt($cData['pass'])),
            'label' => 'Password'
        ),
        array(
            'type' => 'text',
            'name' => 'cryptSalt',
            'value' => post('cryptSalt', $CRYPT->decrypt($cData['cryptSalt'])),
            'label' => 'Crypt Salt'
        ),
        array(
            'type' => 'submit',
            'name' => 'submitBtn',
            'value' => 'Save',
            'label' => '&nbsp;'
        )
    );

    echo JFX::makeRapidForm($formFields, 'Edit connection details', 'edit-connection');


}






















